import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/widgets/comm_app_bar.dart';
import 'package:baselib/widgets/comm_click_widget.dart';
import 'package:baselib/widgets/gradient_card_view.dart';
import 'package:baselib/widgets/inkwell_button.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/comm_widget_manager.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:get/get.dart';
import 'package:module_business/module_my/viewmodel/my_view_model.dart';
/**
 * 注册主页
 */
class MyPage extends GetWidget<MyViewModel> {
  @override
  Widget build(BuildContext context) {
    Get.put(MyViewModel());
    return PageWidget.build(
        builder: (MyViewModel model){
          return PageWidget.PageScaffoldWidget(
            isShowBg: true,
            // appBar: CommAppBar(
            //   leading: SizedBox(width: PxUtil.instance.px_0,),
            //   backgroundColor: ColorUtil.instance.color_transparent,
            // ),

            body: RefreshUtil.init(
              model.refreshController,
                  ()=>model.onRefresh(pageCur: model?.page=1),
                  ()=>model.onRefresh(pageCur: model?.page+1),
              _buildListUi(model),
              physics: ClampingScrollPhysics(),
              loadStatus: model?.loadStatus,
              enablePullDown: true,
              enablePullUp: false,
              isCanMoreData: model?.isCanMoreData??true,
              onReFreshFunction:()=>model.onRefresh(pageCur: model?.page=1),
            ),
          );
        }
    );
  }

  Widget _buildListUi(MyViewModel model) {
    return ListView(
      shrinkWrap: true,
      children: [
        //顶部个人信息
        _buildTopPersonInfoUi(model),
        SizedBox(height: PxUtil.instance.px_18,),
        //fans、follow、friends、visitor
        _buildFansFoUi(model),
        SizedBox(height: PxUtil.instance.px_14,),
        //My Banlance
        InkWellButton(
          onPressed: ()=>model.onFuncItemTap(StringUtil.instance.My_Banlance),
          child: Container(
            margin: EdgeInsets.only(
              left: PxUtil.instance.px_15,
              right: PxUtil.instance.px_15,
            ),
            child: Stack(
              alignment: Alignment.centerLeft,
              children: [
                ImagesUtil.loadAssetImage(
                    DrawableUtil.instance.icon_person_my_balance_bg
                ),
                Container(
                  padding: EdgeInsets.only(
                      left: PxUtil.instance.px_30
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '${model.myInfo?.diamondNum??0}',
                        style: TextStyle(
                            color: ColorUtil.instance.color_white,
                            fontSize: PxUtil.instance.px_16,
                            fontWeight: FontWeight.bold
                        ),
                      ),
                      SizedBox(height: PxUtil.instance.px_5,),
                      Text(
                        StringUtil.instance.My_Banlance,
                        style: TextStyle(
                            color: ColorUtil.instance.color_half_white,
                            fontSize: PxUtil.instance.px_16,
                            fontWeight: FontWeight.w500
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
        SizedBox(height: PxUtil.instance.px_20,),
        //功能列表
        Container(
          margin: EdgeInsets.only(
            left: PxUtil.instance.px_15,
            right: PxUtil.instance.px_15,
          ),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(PxUtil.instance.px_8),
            color: ColorUtil.instance.color_394676,
          ),
          child: ListView.builder(
              padding: EdgeInsets.only(),
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              itemCount: model.funcModels.length,
              itemBuilder: (BuildContext context,int index){
                var funcModel = model.funcModels[index];
                return InkWellButton(
                  onPressed: ()=>model.onFuncItemTap(funcModel.title),
                  child: Container(
                    width: PxUtil.instance.screenWidth,
                    padding: EdgeInsets.only(
                      left: PxUtil.instance.px_20,
                      right: PxUtil.instance.px_16,
                      top: PxUtil.instance.px_16,
                      bottom: PxUtil.instance.px_16,
                    ),
                    // color: ColorUtil.instance.color_394676,
                    child: Row(
                      children: [
                        ImagesUtil.loadAssetImage(
                            funcModel.preImgPath,
                            height: PxUtil.instance.px_20,
                            fit: BoxFit.fitHeight
                        ),
                        SizedBox(width: PxUtil.instance.px_15,),
                        Expanded(child: Text(
                          funcModel.title,
                          style: TextStyle(
                              color: ColorUtil.instance.color_half_white,
                              fontWeight: FontWeight.w500,
                              fontSize: PxUtil.instance.px_16
                          ),
                        )),
                        ImagesUtil.loadAssetImage(
                            DrawableUtil.instance.icon_person_arrow_right,
                            width: PxUtil.instance.px_24
                        )
                      ],
                    ),
                  ),
                );
              }
          ),
        ),
        SizedBox(height: PxUtil.instance.px_20,),
        //Log Out、Delete Account
        Container(
          margin: EdgeInsets.only(
            left: PxUtil.instance.px_15,
            right: PxUtil.instance.px_15,
          ),
          child: Row(
            children: [
              Expanded(child: InkWellButton(
                onPressed: ()=>model.logOut(),
                child: GradientCardView(
                  elevation: PxUtil.instance.px_0,
                  gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                      colors: [
                        ColorUtil.instance.color_7B05FE,
                        ColorUtil.instance.color_BC43FF,
                      ]
                  ),
                  padding: EdgeInsets.only(
                    left: PxUtil.instance.px_22,
                    right: PxUtil.instance.px_22,
                    top: PxUtil.instance.px_13,
                    bottom: PxUtil.instance.px_13,
                  ),
                  borderRadius: BorderRadius.circular(
                      PxUtil.instance.px_26),
                  child: Center(
                    child: Text(
                      StringUtil.instance.Log_Out,
                      style: TextStyle(
                          color: ColorUtil.instance.color_white,
                          fontSize: PxUtil.instance.px_14
                      ),
                    ),
                  ),
                ),
              )),
              SizedBox(width: PxUtil.instance.px_15,),
              Expanded(child: InkWellButton(
                onPressed: ()=>model.deleteAccount(),
                child: GradientCardView(
                  elevation: PxUtil.instance.px_0,
                  gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                      colors: [
                        ColorUtil.instance.color_26F1EB,
                        ColorUtil.instance.color_2075FC,
                      ]
                  ),
                  padding: EdgeInsets.only(
                    left: PxUtil.instance.px_22,
                    right: PxUtil.instance.px_22,
                    top: PxUtil.instance.px_13,
                    bottom: PxUtil.instance.px_13,
                  ),
                  borderRadius: BorderRadius.circular(
                      PxUtil.instance.px_26),
                  child: Center(
                    child: Text(
                      StringUtil.instance.Delete_Account,
                      style: TextStyle(
                          color: ColorUtil.instance.color_white,
                          fontSize: PxUtil.instance.px_14
                      ),
                    ),
                  ),
                ),
              )),
            ],
          ),
        ),
        SizedBox(height: PxUtil.instance.px_15,),
      ],
    );
  }

  _buildTopPersonInfoUi(MyViewModel model) {
    return Container(
      child: Stack(
        children: [
          ImagesUtil.loadAssetImage(
              DrawableUtil.instance.icon_person_top_bg,
              width: PxUtil.instance.screenWidth,
              fit: BoxFit.fitWidth
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(height: PxUtil.instance.statusBarHeight+PxUtil.instance.px_10,),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  InkWellButton(
                    onPressed: ()=>model.toProfileEdit(),
                    child: Container(
                      margin: EdgeInsets.only(
                          right: PxUtil.instance.px_17
                      ),
                      child: ImagesUtil.loadAssetImage(
                          DrawableUtil.instance.icon_person_info_edit,
                          width: PxUtil.instance.px_24
                      ),
                    ),
                  )
                ],
              ),
              SizedBox(height: PxUtil.instance.screenHeight/56,),
              //头像
              CommWidgetManager.buildCicleImagWidget(
                model.myInfo?.icon??'',
                placeholder:
                DrawableUtil.instance.icon_default_head,
                width: PxUtil.instance.px_60,
                height: PxUtil.instance.px_60,
                roundRadius: PxUtil.instance.px_30,
                backgroundColor: ColorUtil.instance.color_f2f2f2,
                padding: EdgeInsets.only(),
                margin: EdgeInsets.only(),
              ),
              SizedBox(height: PxUtil.instance.px_10,),
              //名称
              Text(
                model.myInfo?.nickname??'',
                style: TextStyle(
                    color: ColorUtil.instance.color_white,
                    fontSize: PxUtil.instance.px_18,
                    fontWeight: FontWeight.w500
                ),
              ),
              SizedBox(height: PxUtil.instance.px_5,),
              //ID
              Text(
                '${StringUtil.instance.ID} :${model.myInfo?.userId??0}',
                style: TextStyle(
                    color: ColorUtil.instance.color_half_white,
                    fontSize: PxUtil.instance.px_14,
                    fontWeight: FontWeight.w500
                ),
              ),
              SizedBox(height: PxUtil.instance.px_12,),
              //爱好
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    decoration: BoxDecoration(
                        color: ColorUtil.instance.color_FC45BB,
                        borderRadius: BorderRadius.circular(PxUtil.instance.px_14)
                    ),
                    padding: EdgeInsets.only(
                      left: PxUtil.instance.px_15,
                      right: PxUtil.instance.px_15,
                      top: PxUtil.instance.px_3,
                      bottom: PxUtil.instance.px_3,
                    ),
                    child: Text(
                      'Sport',
                      style: TextStyle(
                          color: ColorUtil.instance.color_white,
                          fontSize: PxUtil.instance.px_14,
                          fontWeight: FontWeight.w500
                      ),
                    ),
                  ),
                  SizedBox(width: PxUtil.instance.px_15,),
                  Container(
                    decoration: BoxDecoration(
                        color: ColorUtil.instance.color_87D660,
                        borderRadius: BorderRadius.circular(PxUtil.instance.px_14)
                    ),
                    padding: EdgeInsets.only(
                      left: PxUtil.instance.px_15,
                      right: PxUtil.instance.px_15,
                      top: PxUtil.instance.px_3,
                      bottom: PxUtil.instance.px_3,
                    ),
                    child: Text(
                      'Yogo',
                      style: TextStyle(
                          color: ColorUtil.instance.color_white,
                          fontSize: PxUtil.instance.px_14,
                          fontWeight: FontWeight.w500
                      ),
                    ),
                  )
                ],
              )
            ],
          )
        ],
      ),
    );
  }

  _buildFansFoUi(MyViewModel model) {
    return GradientCardView(
      elevation: PxUtil.instance.px_0,
      margin: EdgeInsets.only(
        left: PxUtil.instance.px_15,
        right: PxUtil.instance.px_15,
      ),
      alignment: Alignment.center,
      borderRadius: BorderRadius.circular(
          PxUtil.instance.px_8),
      child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: ColorUtil.instance.color_2F4072,
            borderRadius: BorderRadius.circular(
                PxUtil.instance.px_8),
          ),
          // padding: EdgeInsets.only(
          //   left: PxUtil.instance.px_12,
          //   right: PxUtil.instance.px_12,
          //   top: PxUtil.instance.px_5,
          //   bottom: PxUtil.instance.px_5,
          // ),
          child: Row(
            children: [
              _buildFansFoItemUi(model,StringUtil.instance.Fans,model.myInfo?.fansNum??0),
              _buildFansFoItemUi(model,StringUtil.instance.Follow,model.myInfo?.followNum??0),
              _buildFansFoItemUi(model,StringUtil.instance.Friends,model.myInfo?.friendNum??0),
              _buildFansFoItemUi(model,StringUtil.instance.Visitor,model.myInfo?.visitorNum??0),
            ],
          )
      ),
    );
  }

  _buildFansFoItemUi(MyViewModel model, String tag, int num) {
    return Expanded(
        child: InkWellButton(
          onPressed: ()=>model.onFuncItemTap(tag),
          child: Container(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(height: PxUtil.instance.px_12,),
                Text(
                  '${num??0}',
                  style: TextStyle(
                      color: ColorUtil.instance.color_white,
                      fontSize: PxUtil.instance.px_16,
                      fontWeight: FontWeight.bold
                  ),
                ),
                SizedBox(height: PxUtil.instance.px_5,),
                Text(
                  tag,
                  style: TextStyle(
                      color: ColorUtil.instance.color_half_white,
                      fontSize: PxUtil.instance.px_13,
                      fontWeight: FontWeight.w500
                  ),
                ),
                SizedBox(height: PxUtil.instance.px_12,),
              ],
            ),
          ),
        )
    );
  }
}
